﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ApplicationBlocks.Updater;
using Microsoft.ApplicationBlocks.Updater.Downloaders;
using Microsoft.ApplicationBlocks.Updater.ActivationProcessors;

namespace WinClient.Core
{
    public class Foo
    {
        List<Manifest> mManifestToBeAdded;
        private ApplicationUpdaterManager updateManager;
        private int applicableManifests;
        public delegate void ActivateCallHandler();

        public Foo()
        {
            mManifestToBeAdded = new List<Manifest>();
            updateManager = ApplicationUpdaterManager.GetUpdater();
            Console.Write( "Last Checked: {0}", updateManager.LastCheck);
            updateManager.DownloadStarted += new DownloadStartedEventHandler(updateManager_DownloadStarted);
            updateManager.DownloadCompleted += new DownloadCompletedEventHandler(updateManager_DownloadCompleted);
            updateManager.ActivationCompleted += new ActivationCompletedEventHandler(updateManager_ActivationCompleted);
            updateManager.PendingUpdatesDetected += new PendingUpdatesDetectedEventHandler(updateManager_PendingUpdatesDetected);
            updateManager.DownloadError += new DownloadErrorEventHandler(updateManager_DownloadError);
       }

        void updateManager_DownloadError(object sender, ManifestErrorEventArgs e)
        {
            Console.WriteLine(e.Exception);
        }

        void updateManager_PendingUpdatesDetected(object sender, PendingUpdatesDetectedEventArgs e)
        {
            Console.WriteLine("Pending Updates detected");
            updateManager.ResumePendingUpdates();
        }

        public void StartUpdate()
        {
         
            Manifest[] manifestList = updateManager.CheckForUpdates();
            applicableManifests = manifestList.Count();

            if (manifestList.Length > 0)
            {
                Console.WriteLine("Number of Files in Manifest[0] : {0}",
                    manifestList[0].Files.Count);

                manifestList.ToList().ForEach(p => p.Apply = true);

                updateManager.Download(manifestList, new TimeSpan(0, 0, 5));
            }
            else
            {
                Console.WriteLine("No new updates available at this time.");
            }
        }


        void updateManager_ActivationCompleted(object sender, ActivationCompleteEventArgs e)
        {
            Console.WriteLine("Updater Activation Completed :{0} and IsSuccessful: {1}", e.Manifest.ManifestId, e.Success);
        }

        private void updateManager_DownloadCompleted(object sender, ManifestEventArgs e)
        {
             mManifestToBeAdded.Add(e.Manifest);
            if (mManifestToBeAdded.Count == this.applicableManifests)
            {
               ActivateCall();
            }
        }

        private void updateManager_DownloadStarted(object sender, DownloadStartedEventArgs e)
        {
            Console.WriteLine("Manifest Download Started: {0}", e.Manifest.ManifestId);
        }

        private void ActivateCall()
        {
            ApplicationUpdaterManager updater = ApplicationUpdaterManager.GetUpdater();
            updater.Activate(this.mManifestToBeAdded.ToArray());
            mManifestToBeAdded.Clear();
        }
    }
}
